﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
public partial class adsetup_TAdSetupInfo1 : System.Web.UI.Page
{
    public int pagesize = 20;
    public string pager = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //TUser1.CheckUser(this);
            Bind();
            if (Request["opration"] != null)
                switch (Request["opration"].ToString())
                {
                    case "bind":
                        BindInput();
                        break;
                    case "edit":
                        Save("edit");
                        break;
                    case "save":
                        Save("save");
                        break;
                    case "del":
                        Del();
                        break;
                }
        }
       

    }

    public void Bind()
    {
        string sSql = "";
        int page = 1;
        if (Request["page"] != null)
        {
            page = int.Parse(Request["page"].ToString());
            sSql = "with a as ( select (select count(*) from t_ad_setup_info1) as countnum, row_number() over(order by orderby desc) as num,* from t_ad_setup_info1 ) select  * from a where num between " + ((page - 1) * pagesize + 1) + " and " + ((page - 1) * pagesize + pagesize);
        }
        else
        {
            sSql = "with a as ( select (select count(*) from t_ad_setup_info1) as countnum, row_number() over(order by orderby desc) as num,* from t_ad_setup_info1 ) select * from a where num between 1 and " + pagesize;
        }
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dlTable.DataSource = myReader.FDataReader;
        dlTable.DataBind();
        if (myReader.FDataReader.HasRows)
        {
            int countnum = TSqlDB.ExecuteScalar(sSql);
            if (countnum % pagesize > 0)
                pager = GenPagebar(page, countnum / pagesize + 1);
            else
                pager = GenPagebar(page, countnum / pagesize);
        }
        
    }
    
    public void Save(string type)
    {
       
            TAdSetupInfo1 oASI1 = new TAdSetupInfo1(Request["id"].ToString());
            oASI1.Select();
            oASI1.TypeClass = Request["class"].ToString();
            oASI1.TypeName = Request["name"].ToString();
            oASI1.Description = Request["desc"].ToString();
            if (Request["show"].ToString() == "true")
                oASI1.IsShow = "1";
            else
                oASI1.IsShow = "0";
            oASI1.OrderBy = decimal.Parse(Request["order"].ToString());

            if (type == "edit")
            {
                oASI1.Update();
            }
            else
            {
                if (oASI1.TypeId == null)
                {
                    oASI1.TypeId = Request["id"].ToString();
                    oASI1.Insert();
                }
                else
                {
                    Response.Clear();
                    Response.Write("代码重复");
                    Response.Flush();
                    Response.Close();
                    Bind();
                    return ;
                }
            }
            Response.Clear();
            Response.Flush();
            Response.Close();
            //Bind();


        
    }
    public void BindInput()
    {
        if (Request["id"] != null)
        {
            TAdSetupInfo1 oASI1 = new TAdSetupInfo1(Request["id"].ToString());
            oASI1.Select();
            Response.Clear();
            Response.Write(oASI1.TypeId+","+oASI1.TypeClass +","+oASI1.TypeName+","+oASI1.Description +","+oASI1.IsShow +","+oASI1.OrderBy );
            Response.Flush();
            Response.Close();
        }
    }
    public void Del()
    {
        if (Request["id"]!=null)
        {
            TAdSetupInfo1 oASI1 = new TAdSetupInfo1(Request["id"].ToString());
            oASI1.Delete();
        }
        Bind();
        
    }


    protected void dlTable_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            TAdSetupInfo1 oASI1 = new TAdSetupInfo1(e.CommandArgument.ToString());
            TSqlDB.ExecuteNonQuery("delete from t_ad_setup_info2 where typeid='" + e.CommandArgument.ToString() + "'");
            oASI1.Delete();
            
        }
        Bind();
    }


    public string GenPagebar(int PageNum, int PageCounts)
    {
        string result = "<div class=\"pagebox\">";


        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a href='TAdSetupInfo1.aspx?opration=page&page=2'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo1.aspx?opration=page&page=1' \">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + (PageNum - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + (PageNum + 1).ToString() + "'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo1.aspx?opration=page&page=1'\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdSetupInfo1.aspx?opration=page&page=" + (PageCounts - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + pagesize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }
    protected void btnPanel2Cancel_Click(object sender, EventArgs e)
    {
        Bind();
    }
}
